package com.cskaoyan.service;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.StorageDeleteBo;
import com.cskaoyan.bean.bo.StorageUpdateBo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.param.FileParam;
import com.cskaoyan.bean.po.StoragePO;
import com.cskaoyan.bean.vo.StorageListInnerVo;
import com.cskaoyan.bean.vo.StorageListVO;

import com.cskaoyan.mapper.StorageMapper;
import com.cskaoyan.util.RandomNameUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.cskaoyan.bean.param.FileParam.BASE_PATH;
// <<<<<<< HEAD
// // <<<<<<< HEAD
// // // import static com.cskaoyan.bean.param.FileParam.PATH_RESOURCES;
// // =======
// //
// // >>>>>>> 40bb8e41f3240769bbde3aea25154e1008e2d58c
// =======
// >>>>>>> 760343478bcb17e710618bfaf785f7a78399b9e9

@Service
public class StorageServiceImpl implements StorageService {
    @Autowired
    StorageMapper storageMapper;

    @Override
    public BaseRespVo create(MultipartFile file) {
        StoragePO storagePO = new StoragePO();
        System.out.println(file.getOriginalFilename());

        String name = RandomNameUtils.getName(Objects.requireNonNull(file.getOriginalFilename()));
        storagePO.setKey(name);
        storagePO.setAddTime(new Date());
        storagePO.setName(file.getOriginalFilename());
        storagePO.setSize((int) file.getSize());
        storagePO.setAdd_time(storagePO.getAddTime());
        storagePO.setUpdateTime(storagePO.getAddTime());
        storagePO.setUpdate_time(storagePO.getUpdateTime());
        storagePO.setType(file.getContentType());
        storagePO.setDeleted(false);
        String url = FileParam.PIC_APP + name;
        storagePO.setUrl(url);
        String property = System.getProperty("os.name");
        File path = null;
        if (property.contains("Windows")) {
            path = new File(BASE_PATH + FileParam.PATH_NAME_WINDOWS,name);
        } else if (property.contains("Mac")) {
            path = new File(BASE_PATH + FileParam.PATH_NAME_MAC,name);
        }
        try {

            assert path != null;
            System.out.println(name);

            // BASE_PATH + FileParam.PATH_NAME_WINDOWS,name
            // 保存到   /Users/chy/xuexi39th/08Spring/zoye/seeonproject/target/classes/static/0f0254a191082854478d67d662eda2b9.jpeg
            file.transferTo(path);


            // BASE_PATH+"/src/main/resources/static/",name)
            // 保存到  /Users/chy/xuexi39th/08Spring/zoye/seeonproject/src/main/resources/static/0f0254a191082854478d67d662eda2b9.jpeg
            // file.transferTo(new File(BASE_PATH+"/src/main/resources/static/",name));



             // * 在复制到 path 的同时,也复制到   开发路径下的resources目录 ;
             // * chy 2022年06月07日08:56:42
             // */
            // File resources = new File(BASE_PATH + PATH_RESOURCES);
            // File resources = new File("/Users/chy/xuexi39th/08Spring/zoye/seeonproject/src/main/resources/static/");
            // File resources = new File(FileParam.getSavePath());
            // File resources = new File(BASE_PATH + "/src/main/resources/", name);
            // if(resources==null &&  resources.equals("") ){
            //     System.out.println("1");
            //     throw  new FileNotFoundException("文件不存在");
            //
            // }
            // System.out.println("2");
            // file.transferTo(resources);

        } catch (IOException e) {
            e.printStackTrace();
        }
        int affectRows = storageMapper.insertImage(storagePO);
        storagePO.setUrl(FileParam.URL_PRE + storagePO.getUrl());
        // storagePO.setId();
        return BaseRespVo.ok(storagePO);

    }

    /**
     * 实现/admin/storage/list 接口;
     * 2022年06月06日19:41:05 chy
     * @param baseParam
     * @param key
     * @param name
     * @return
     */
    @Override
    public StorageListVO list(BaseParam baseParam, String key, String name) {
        // 1. 开启分页;
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        // 直接返回一个 StorageListVO对象;
        StorageListVO storageListVO   =    new StorageListVO();
          List<StorageListInnerVo> storageListInnerVo  =  storageMapper.queryAllByParam(baseParam,key,name);
        /**
         * 拼接 url 与 host;
         * FileParam.URL_PRE
         */
     // List<StorageListInnerVo>  list  = new ArrayList<StorageListInnerVo>();

        for (int i = 0; i < storageListInnerVo.size(); i++) {
            StorageListInnerVo temp    =      storageListInnerVo.get(i);
            temp.setUrl(FileParam.URL_PRE+temp.getUrl());
        }

        PageInfo pageInfo = new PageInfo(storageListInnerVo);
        storageListVO.setPage(baseParam.getPage());
        storageListVO.setLimit(baseParam.getLimit());
        storageListVO.setList(storageListInnerVo);
        storageListVO.setPages(pageInfo.getPages());
        storageListVO.setTotal(pageInfo.getTotal());
        return storageListVO;
    }

    /**
     * 删除 storage delete  假删除;
     * @param storageDeleteBo
     */
    @Override
    public void delete(StorageDeleteBo storageDeleteBo) {
             storageMapper.delete(storageDeleteBo);

    }

    /**
     * 复用 VXCollectAddOrDeleteBo 为 po;
     * 因为无需设计到 mysql 里类型和 java  里类型 的转化:如 mysql :varchar  和 java Integer[];
     * @param storageUpdateBo
     * @return
     */
    @Override
    public StorageUpdateBo update(StorageUpdateBo storageUpdateBo) {
        //VXCollectAddOrDeleteBo ->Po;
      int count =     storageMapper.update(storageUpdateBo);

        // StorageUpdateVo   storageUpdateVo  = new StorageUpdateVo();
        // storageUpdateVo.setId()
        // 继续复用 VXCollectAddOrDeleteBo 作为 Vo;
        storageUpdateBo.setUpdateTime(new Date());

        return storageUpdateBo;
    }
}
